package com.cloudcc.mobile.view.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.NewMoreAdapter;
import com.cloudcc.mobile.adapter.NewMoreForAssetsAdapter;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.litesuits.android.log.Log;
import com.mypage.model.GongNiuZiChan;

import org.kymjs.kjframe.ui.ViewInject;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;

/**
 * <pre>
 *     author : 92152
 *     e-mail : xxx@xx
 *     time   : 2020/05/08
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class NewMoreForAssetsActivity extends BaseActivity implements NewMoreForAssetsAdapter.CallBackListen{
    @Bind(R.id.new_selece_btn)
    Button guanbibtn;
    @Bind(R.id.new_selece_listview)
    ListView mListview;
    @Bind(R.id.new_selece_ll)
    LinearLayout new_selece_ll;

    NewMoreForAssetsAdapter adapter;
    String mEntityId,mEntityNameID,from,Mshijian,jsonname,mPname,mObjId,mObjName;

    public String mPersionName;
    public String mPersionNameIds;

    //	private BeauInfoEntity mBeauInfo;
    public List<BeauInfoEntity.TabLabel> tabLabel;
    List<BeauInfoEntity.ObjMenu> jsonObject;
    List<BeauInfoEntity.ObjChange> mJot;

    //营销名字
    private String listTitle;

    boolean isMeFollow = false;
    ProgressDialog dialog2;
    //所属门店
    public String ssmendian;
    //投放地址
    public String tfdz;
    //名字
    public String name1;
    //资产名称
    public String zcmc;
    //资产类型
    public String zclx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addListener();
        addData();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        RunTimeManager.getInstance().setIsrem(false);
        finish();
        return super.onTouchEvent(event);
    }
    private void addData() {
        listTitle = getIntent().getExtras().getString("listTitle");
        mPersionName = getIntent().getStringExtra("mPersionName");
        mPersionNameIds = getIntent().getStringExtra("mPersionNameIds");

        mPname = getIntent().getStringExtra("mPname");
        mObjId = getIntent().getStringExtra("mObjId");
        mObjName = getIntent().getStringExtra("mObjName");
        mEntityId = getIntent().getStringExtra("mEntityId");
        mEntityNameID = getIntent().getStringExtra("mEntityNameID");
        jsonObject = (List<BeauInfoEntity.ObjMenu>) getIntent().getSerializableExtra("mBeauInfo.relationCreateUrl");
        mJot = (List<BeauInfoEntity.ObjChange>)getIntent().getSerializableExtra("mBeauInfo.recordIdButton");
        tabLabel = (List<BeauInfoEntity.TabLabel>) getIntent().getSerializableExtra("mBeauInfo.tabLabel");
        isMeFollow = getIntent().getBooleanExtra("beau.ismefollow", false);
        from = getIntent().getStringExtra("from");
        Mshijian = getIntent().getStringExtra("mShiJian");
        jsonname = getIntent().getStringExtra("jsonname");
        adapter = new NewMoreForAssetsAdapter(mPersionName,mPersionNameIds,mJot,mContext,mEntityId,mEntityNameID,jsonObject,from,isMeFollow,Mshijian,jsonname,tabLabel,mPname,mObjId,mObjName);
        adapter.setListener(this);
        mListview.setAdapter(adapter);

        ViewTreeObserver vto = mListview.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mListview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                if(mListview.getHeight()>1500){
                    mListview.setLayoutParams(
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1500));
                }
                mListview.getWidth();
            }
        });
//        if(5<jsonObject.size()){
//			mListview.setLayoutParams(
//	                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1500));
//		}
    }

    private void addListener() {
        guanbibtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                RunTimeManager.getInstance().setIsrem(false);
                finish();
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.new_more;
    }

    @Override
    public void CallBack() {
        requestNumNo(listTitle);
    }

    public void requestNumNo(final String name) {
        dialog2=new ProgressDialog(this);
        dialog2.show();
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance()
                .getServerBinding());
        params.addBodyParameter("serviceName", "cquery");
        params.addBodyParameter("objectApiName", "yxzc");
        params.addBodyParameter("expressions", "name=" + "'" + name + "'");
        Log.d("列表值url-------------------------", UrlManager.getInterfaceUrl()
                + "?binding=" + RunTimeManager.getInstance().getServerBinding()
                + "&serviceName=getLookupValue"
                + "&objectId=2016360343BD7B2SQ2PO" + "&pageSize=1"
                + "&keyword=");
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.d("列表值接口失败-------------------------", arg1);
                        Toast.makeText(NewMoreForAssetsActivity.this, "资产出错", Toast.LENGTH_SHORT).show();
                        hideProgress();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        Log.d("列表值接口成功-------------------------", arg0.result);
                        Gson gson = new Gson();
                        GongNiuZiChan mData = gson.fromJson(arg0.result.toString(),
                                GongNiuZiChan.class);
                        if (mData.data.size() <= 0) {
                            Toast.makeText(NewMoreForAssetsActivity.this, "该资产编码不存在", Toast.LENGTH_SHORT).show();
                            //finish();
                        } else {
                            mData.data.get(0);
                            ssmendian = mData.data.get(0).ssmendianccname;
                            tfdz = mData.data.get(0).tfdz;
                            name1 = mData.data.get(0).name;
                            zcmc = mData.data.get(0).zcmc;
                            zclx = mData.data.get(0).zclx;

                            requestNum(name,mData);
                        }
                    }
                });
    }

    public void requestNum(String name, final GongNiuZiChan mDatax) {

        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance()
                .getServerBinding());
        params.addBodyParameter("serviceName", "cqueryWithRoleRight");
        params.addBodyParameter("objectApiName", "yxzc");
        params.addBodyParameter("expressions", "name=" + "'" + name + "'");
        Log.d("列表值url-------------------------", UrlManager.getInterfaceUrl()
                + "?binding=" + RunTimeManager.getInstance().getServerBinding()
                + "&serviceName=getLookupValue"
                + "&objectId=2016360343BD7B2SQ2PO" + "&pageSize=1"
                + "&keyword=");
        Log.d("request", UrlManager.getInterfaceUrl() + "serviceName=cqueryWithRoleRight" + "objectApiName=yxzc" + "expressions=name="+"'"+name+"'" + "binding=" + RunTimeManager.getInstance()
                .getServerBinding());
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.d("列表值接口失败-------------------------", arg1);
                        hideProgress();
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        hideProgress();
                        Log.d("列表值接口成功-------------------------", arg0.result);
                        Gson gson = new Gson();
                        GongNiuZiChan mData = gson.fromJson(arg0.result.toString(),
                                GongNiuZiChan.class);
                        if (mData.data.size() <= 0) {
                            Toast.makeText(NewMoreForAssetsActivity.this, "该资产无需巡检", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            if (null == mData.data.get(0).ssjxsccname || "".equals(mData.data.get(0).ssjxsccname)) {
                                Toast.makeText(NewMoreForAssetsActivity.this, "该资产无需巡检", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                Intent intent = new Intent(NewMoreForAssetsActivity.this, com.cloudcc.mobile.bull.BullXunjianActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("gongniudata", (Serializable) mDatax.data.get(0));
                                intent.putExtras(bundle);
//                                intent.putExtra("ssmendian",ssmendian);
//                                intent.putExtra("tfdz",tfdz);
//                                intent.putExtra("name1",name1);
//                                intent.putExtra("zcmc",zcmc)
//                                intent.putExtra("zclx",zclx);
                                intent.putExtra("ssjxs",mData.data.get(0).ssjxsccname);
                                startActivity(intent);
                                finish();
                            }
                        }
                    }
                });
    }

    public void hideProgress() {
        if (dialog2 != null) {
            if (dialog2.isShowing()) { //check if dialog is showing.

                //get the Context object that was used to great the dialog
                Context context = ((ContextWrapper) dialog2.getContext()).getBaseContext();

                //if the Context used here was an activity AND it hasn't been finished or destroyed
                //then dismiss it
                if (context instanceof Activity) {
                    if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
                        dialog2.dismiss();
                } else //if the Context used wasnt an Activity, then dismiss it too
                    dialog2.dismiss();
            }
            dialog2 = null;
        }
    }
    }
